﻿// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
//using System.Text;

namespace Petra.Gis.ReferenceSystemServices
{
  /// <summary>
  /// The Spheroid defines the standard information stored with ellipsoid objects.
  /// </summary>
  public class Spheroid
  {

    private Metadata _metadata;
    private double _eccentricity;
    private double _eccentricitySquared;


    /// <summary>
    /// xaml constr.
    /// </summary>
    public Spheroid() { }





    /// <summary>
    /// Initializes a new instance of an Ellipsoid
    /// </summary>
    /// <param name="semiMajorAxis">Semi major axis</param>
    /// <param name="semiMinorAxis">Semi minor axis</param>
    /// <param name="inverseFlattening">Inverse flattening</param>
    /// <param name="isIvfDefinitive">Inverse Flattening is definitive for this ellipsoid (Semi-minor axis will be overridden)</param>
    /// <param name="axisUnit">Axis unit</param>
    /// <param name="name">Name</param>
    /// <param name="authority">Authority name</param>
    /// <param name="code">Authority-specific identification code.</param>
    /// <param name="alias">Alias</param>
    /// <param name="abbreviation">Abbreviation</param>
    /// <param name="remarks">Provider-supplied remarks</param>
    public Spheroid(
      double semiMajorAxis,
      double semiMinorAxis,
      double inverseFlattening,
      bool isIvfDefinitive,
      DistanceUnit axisUnit, Metadata metadata)
      {
        if (metadata == null)
          throw new ArgumentException("invalid metadata");

      _SemiMajorAxis = semiMajorAxis;
      _InverseFlattening = inverseFlattening;
      _AxisUnit = axisUnit;
      _IsIvfDefinitive = isIvfDefinitive;
      if (isIvfDefinitive)
        _SemiMinorAxis = (1.0 - (1.0 / _InverseFlattening)) * semiMajorAxis;
      else
        _SemiMinorAxis = semiMinorAxis;
    }



    //public Ellipsoid(string name, Distance equatorialRadius, Distance polarRadius)
    //{
    //    this.pName = name;
    //    this.pEquatorialRadius = equatorialRadius;
    //    this.pPolarRadius = polarRadius;
    //    this.Calculate();
    //    pEllipsoids.Add(this);
    // }

    //private void Calculate()
    //{
    //    this.pFlattening = (this.pEquatorialRadius.ToMeters().Value - this.pPolarRadius.ToMeters().Value) / this.pEquatorialRadius.ToMeters().Value;
    //    this.pInverseFlattening = 1.0 / this.pFlattening;
    //    this.pEccentricity = Math.Sqrt((Math.Pow(this.pEquatorialRadius.Value, 2.0) - Math.Pow(this.pPolarRadius.Value, 2.0)) / Math.Pow(this.pEquatorialRadius.Value, 2.0));
    //    this.pEccentricitySquared = Math.Pow(this.Eccentricity, 2.0);
    //}




    public Spheroid(string name, double equatorialRadius, 
                    double polarRadius, bool isIvfDefinitive, 
                    DistanceUnit axisUnit)
    {
      this.Metadata = new Metadata(name, "", -1, "", name,"");
      _SemiMajorAxis = equatorialRadius;
      _SemiMinorAxis = polarRadius;

      Calculate();

      if (isIvfDefinitive)
        _SemiMinorAxis = (1.0 - (1.0 / _InverseFlattening)) * _SemiMajorAxis;
      else
        _SemiMinorAxis = polarRadius;


      Spheroids.SearchEllipsoid.Add(this);
    }



    private void Calculate()
    {
      _InverseFlattening = (_SemiMajorAxis - _SemiMinorAxis) / _SemiMajorAxis;
      _InverseFlattening = 1.0 / _InverseFlattening;
      _eccentricity = Math.Sqrt((Math.Pow(_SemiMajorAxis, 2.0) - Math.Pow(_SemiMinorAxis, 2.0)) / Math.Pow(_SemiMajorAxis, 2.0));
      _eccentricitySquared = Math.Pow(_eccentricity, 2.0);
    }


    #region Predefined ellipsoids
    /// <summary>
    /// WGS 84 ellipsoid
    /// </summary>
    /// <remarks>
    /// Inverse flattening derived from four defining parameters 
    /// (semi-major axis;
    /// C20 = -484.16685*10e-6;
    /// earth's angular velocity w = 7292115e11 rad/sec;
    /// gravitational constant GM = 3986005e8 m*m*m/s/s).
    /// </remarks>


    /// <summary>
    /// WGS 72 Ellipsoid
    /// </summary>
 

    /// <summary>
    /// GRS 1980 / International 1979 ellipsoid
    /// </summary>
    /// <remarks>
    /// Adopted by IUGG 1979 Canberra.
    /// Inverse flattening is derived from
    /// geocentric gravitational constant GM = 3986005e8 m*m*m/s/s;
    /// dynamic form factor J2 = 108263e8 and Earth's angular velocity = 7292115e-11 rad/s.")
    /// </remarks>


    /// <summary>
    /// International 1924 / Hayford 1909 ellipsoid
    /// </summary>
    /// <remarks>
    /// Described as a=6378388 m. and b=6356909m. from which 1/f derived to be 296.95926. 
    /// The figure was adopted as the International ellipsoid in 1924 but with 1/f taken as
    /// 297 exactly from which b is derived as 6356911.946m.
    /// </remarks>


    /// <summary>
    /// Clarke 1880
    /// </summary>
    /// <remarks>
    /// Clarke gave a and b and also 1/f=293.465 (to 3 decimal places).  1/f derived from a and b = 293.4663077
    /// </remarks>
 

    /// <summary>
    /// Clarke 1866
    /// </summary>
    /// <remarks>
    /// Original definition a=20926062 and b=20855121 (British) feet. Uses Clarke's 1865 inch-metre ratio of 39.370432 to obtain metres. (Metric value then converted to US survey feet for use in the United States using 39.37 exactly giving a=20925832.16 ft US).
    /// </remarks>


    /// <summary>
    /// Sphere
    /// </summary>
    /// <remarks>
    /// Authalic sphere derived from GRS 1980 ellipsoid (code 7019).  (An authalic sphere is
    /// one with a surface area equal to the surface area of the ellipsoid). 1/f is infinite.
    /// </remarks>
 
    #endregion


    #region Ellipsoid Members


    private double _SemiMajorAxis;

    /// <summary>
    /// Gets or sets the value of the semi-major axis.
    /// </summary>
    public double SemiMajorAxis
    {
      get { return _SemiMajorAxis; }
      set { _SemiMajorAxis = value; }
    }




    private double _SemiMinorAxis;

    /// <summary>
    /// Gets or sets the value of the semi-minor axis.
    /// </summary>
    public double SemiMinorAxis
    {
      get { return _SemiMinorAxis; }
      set { _SemiMinorAxis = value; }
    }




    private double _InverseFlattening;

    /// <summary>
    /// Gets or sets the value of the inverse of the flattening constant of the ellipsoid.
    /// </summary>
    public double InverseFlattening
    {
      get { return _InverseFlattening; }
      set { _InverseFlattening = value; }
    }




    private DistanceUnit _AxisUnit;

    /// <summary>
    /// Gets or sets the value of the axis unit.
    /// </summary>
    public DistanceUnit AxisUnit
    {
      get { return _AxisUnit; }
      set { _AxisUnit = value; }
    }




    private bool _IsIvfDefinitive;

    /// <summary>
    /// Tells if the Inverse Flattening is definitive for this ellipsoid. Some ellipsoids use 
    /// the IVF as the defining value, and calculate the polar radius whenever asked. Other
    /// ellipsoids use the polar radius to calculate the IVF whenever asked. This 
    /// distinction can be important to avoid floating-point rounding errors.
    /// </summary>
    public bool IsIvfDefinitive
    {
      get { return _IsIvfDefinitive; }
      set { _IsIvfDefinitive = value; }
    }




    public Metadata Metadata
    {
      get { return _metadata; }
      set { _metadata = value; }
    }




    public double Eccentricity
    {
      get { return _eccentricity; }
    }


    #endregion

    /// <summary>
    /// Checks whether the values of this instance is equal to the values of another instance.
    /// Only parameters used for coordinate system are used for comparison.
    /// Name, abbreviation, authority, alias and remarks are ignored in the comparison.
    /// </summary>
    /// <param name="obj"></param>
    /// <returns>True if equal</returns>
    public bool EqualParams(object obj)
    {
      if (!(obj is Spheroid))
        return false;
      Spheroid e = obj as Spheroid;
      return (e.InverseFlattening == this.InverseFlattening &&
          e.IsIvfDefinitive == this.IsIvfDefinitive &&
          e.SemiMajorAxis == this.SemiMajorAxis &&
          e.SemiMinorAxis == this.SemiMinorAxis &&
          e.AxisUnit.EqualParams(this.AxisUnit));
    }
  }
}
